package com.hopes.bizz.protocol.hj212.server;

import com.hopes.bizz.protocol.hj212.server.handler.HJ212InboundHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * @author jie.dai
 */
@Component
public class ServerInitHandler extends ChannelInitializer<NioSocketChannel> {

    @Autowired
    private HJ212InboundHandler hj212InboundHandler;

    @Override
    protected void initChannel(NioSocketChannel channel) {
        StringDecoder decoder = new StringDecoder(StandardCharsets.UTF_8);
        StringEncoder encoder = new StringEncoder(StandardCharsets.UTF_8);
        channel.pipeline()
                .addLast(new LineBasedFrameDecoder(Integer.MAX_VALUE))
                .addLast("decoder", decoder)
                .addLast("encoder", encoder)
                .addLast(hj212InboundHandler)
        ;

    }
}
